package edu.ciee.tz.dao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import edu.ciee.tz.model.Truck;



@Component("truckDao")
public class TruckDAO {

	public List<Truck> search(Truck t) throws Exception {

		String query = "where 1=1";

		if (t.getChepai()!= null) 
			query = query + " and chepai like '%" + t.getChepai()+ "%'";// this.getID()
		if (t.getTruck_type()!= null) 
			query = query + " and truck_type like '%" + t.getTruck_type()+ "%'"; // this.getName()
		if (t.getSize() != 0) 
			query = query + " and size like '%" + t.getSize()+ "%'"; // this.getName()
		if (t.getGouru_time()!= null) 
			query = query + " and gouru_time like '%" + t.getGouru_time() + "%'"; // this.getName()
		if (t.getLaiyuan()!=null&&t.getLaiyuan().getId()!= 0) 
			query = query + " and laiyuan like '%" + t.getLaiyuan().getId() + "%'"; // this.getName()
		
		@SuppressWarnings("unchecked")
		List<Truck> t_list= hibernateTemplate.find("from Truck u "+ query);

		return t_list;
	}

	public void insert(Truck t) throws Exception {
		this.getHibernateTemplate().save(t);
	}

	public void delete(Truck t) throws Exception {
		this.getHibernateTemplate().delete(t);

	}

	public void update(Truck t) throws Exception {
		this.getHibernateTemplate().update(t);
	}

	private HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
}
